package com.major.dawn.leetCode.tree;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;

import java.util.LinkedList;
import java.util.List;

public class FlattenTree {

    public void flatten(TreeNode root) {

        if (root == null) {
            return;
        }

        LinkedList<TreeNode> stack = new LinkedList<>();
        stack.push(root);
        TreeNode curr = root;

        while (!stack.isEmpty()) {
            TreeNode poll = stack.poll();

            if (poll != root) {
                TreeNode treeNode = new TreeNode(poll.val);
                curr.right = treeNode;
                curr = treeNode;
            }

            TreeNode left = poll.left;
            TreeNode right = poll.right;
            if (right != null) {
                stack.push(right);
            }
            if (left != null) {
                stack.push(left);

            }
        }

    }


}
